using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Index
{
    /// <summary>
    /// Reprezentacja pliku z z zapisanymi listami postingowymi
    /// </summary>
    public class IndexFile
    {
        private String path;
        private FileStream stream;

        public IndexFile(String path)
        {
            this.path = path;
        }

        public void Open()
        {
            this.stream = new FileStream(path, FileMode.OpenOrCreate);
        }

        public byte[] ReadBytes(long start_addr, long end_addr)
        {
            byte[] result = new byte[end_addr - start_addr + 1];
            this.stream.Seek( start_addr - this.stream.Position, SeekOrigin.Current);
            this.stream.Read(result, 0, result.Length);
            return result;
        }

        public void AppendBytes(out long start_addr, out long end_addr, byte[] bytes)
        {
            start_addr = this.stream.Position;
            this.stream.Write(bytes, 0, bytes.Length);
            end_addr = this.stream.Position - 1;
        }
       
        public void Flush()
        {
            stream.Flush();
        }

        public void Close()
        {
            stream.Flush();
            stream.Close();
        }
    }
}
